home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / local / bin / tails-start-i2p < prev    next >
Encoding:
Text File  |  2012-08-03  |  3.2 KB  |  134 lines

  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. #man{{{
  7.  
  8. =head1 NAME
  9.  
  10. tails-start-i2p
  11.  
  12. =head1 VERSION
  13.  
  14. Version X.XX
  15.  
  16. =head1 AUTHOR
  17.  
  18. Tails dev team <amnesia@boum.org>
  19. See https://tails.boum.org/.
  20.  
  21. =cut
  22.  
  23. #}}}
  24.  
  25. use Desktop::Notify;
  26. use Locale::gettext;
  27. use POSIX;
  28.  
  29. ### initialization
  30. setlocale(LC_MESSAGES, "");
  31. textdomain("tails-i2p-notify-user");
  32.  
  33. ### helper subs
  34.  
  35. # TODO: get router port (default 7657) from /etc/i2p/clients.config
  36. sub get_router_port {
  37.     return 7657;
  38. }
  39.  
  40. # TODO: more perlish way to do below?
  41. # TODO: use netstat -p, check that a child of i2psvc runs the router console
  42. sub router_status {
  43.     return !system("netstat -nl -A inet,inet6 | grep -qe \"\\(127\\.0\\.0\\.1\\|::1\\):" . get_router_port() . "\"");
  44. }
  45.  
  46. sub open_router_console {
  47.     system("/usr/bin/iceweasel http://127.0.0.1:" . get_router_port());
  48. }
  49.  
  50. sub start_i2psvc {
  51.     system("/usr/bin/gksu /etc/init.d/i2p start");
  52. }
  53.  
  54. sub stop_i2psvc {
  55.     system("/usr/bin/gksu /etc/init.d/i2p start");
  56. }  
  57.  
  58. ### main
  59.  
  60. my $notify = Desktop::Notify->new();
  61.  
  62. my $summary = gettext("Starting I2P...");
  63. my $body    = gettext("The I2P router console will be opened on start.");
  64.  
  65. my $notification = $notify->create(summary => $summary,
  66.                                    body => $body,
  67.                                    timeout => 0);
  68.  
  69. $notification->show();
  70.  
  71. my $tordate_done_file = '/var/run/tordate/done';
  72. my $tordate_wait      = 0;
  73.  
  74. # There was a "fix" in i2p 0.8.8 for handling clock jumps and skews which seems
  75. # to be broken -- a jump during i2p bootstrap leads to i2p starting in a non-
  76. # working state, as does starting i2p when the clock is off too much. Hence, for
  77. # simplicity, we make i2p dependent on tordate. The real fix will be when
  78. # i2p gets its act together and handles these problems correctly.
  79. until (-e $tordate_done_file) {
  80.     if ($tordate_wait > 60) {
  81.     $notification->close();
  82.     $summary = gettext("I2P failed to start");
  83.     $body    = gettext("Make sure that you have a working Internet " .
  84.                   "connection, then try to start I2P again.");
  85.     $notification = $notify->create(summary => $summary,
  86.                        body => $body,
  87.                        timeout => 60000);
  88.     $notification->show();
  89.     exit 1;
  90.     }
  91.     sleep(1);
  92.     $tordate_wait++;
  93. }
  94.  
  95. my $htpdate_done_file = '/var/run/htpdate/done';
  96. my $htpdate_wait      = 0;
  97.  
  98. # We also need to wait for htpdate for same the reason as
  99. # above. However, tordate will set the clock so that it is correct
  100. # enough for I2P to work (it can operate with +/- 2 hours clock skew)
  101. # so we optimistically try to start I2P even if htpdate doesn't
  102. # finish.
  103. until (-e $htpdate_done_file || $htpdate_wait > 120) {
  104.     sleep(1);
  105.     $htpdate_wait++;
  106. }
  107.  
  108. start_i2psvc();
  109.  
  110. my $t = 0;
  111. my $timeout = 180;
  112. while ($t < $timeout && !router_status()) {
  113.     $t++;
  114.     sleep 1;
  115. }
  116.  
  117. $notification->close();
  118.  
  119. if (router_status()) {
  120.     open_router_console();
  121.     exit 0;
  122. } else {
  123.     stop_i2psvc();
  124.     $summary = gettext("I2P failed to start");
  125.     $body    = gettext("Something went wrong when I2P was starting. Look in " .
  126.                "the logs in the following directory for " .
  127.               "more information:") . "\n\t/var/log/i2p/";
  128.     $notification = $notify->create(summary => $summary,
  129.                                     body => $body,
  130.                                     timeout => 60000);
  131.     $notification->show();
  132.     exit 1;
  133. }
  134.